/* Copyright  2009
   @Author
   Richard Changde Yin            e-mail yinchangde@hotmail.com

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/**
 * @filename: scheduling.cc
 */
#include <server_includes.h>
#include <scheduling.h>
#include <gettext.h>
//#include <connect.h>

Scheduler *thread_scheduler= NULL;
/* We must insist that only one of these plugins get loaded at a time */

static bool scheduler_inited= false;

/* opt_scheduler declared in the main.cc */
extern char *opt_scheduler;

Scheduler&   get_thread_scheduler()
{
  assert(thread_scheduler != NULL);
  return *thread_scheduler;
}

/**
 * SYNOPSIS: scheduling_initializer
 * @param    plugin      st_plugin_int*
 * @return   0 OK
 *           1 error
 */
int scheduling_initializer(st_plugin_int* plugin)
{
  errmsg_printf(ERRMSG_LVL_INFO, _("Initialize scheduling plugin '%s'.\n"),
                     plugin->name.str);
  /*       comparing the scheduler name
   *       comment out for now.
  if (::memcmp(plugin->plugin->name, opt_scheduler, strlen(opt_scheduler)))
    return 0;
  */
  if (scheduler_inited)
  {
    fprintf(stderr, "Scheduler plugin has been initialized already!\n");
    fprintf(stderr, "Only one scheduler plugin is allowed to be initialized\n");
    exit(1);
  }

  assert(plugin->plugin->init); /* Find poorly designed plugins */

  if (plugin->plugin->init((void *)&thread_scheduler))
  {
    /*
      TRANSLATORS> The leading word "scheduling" is the name
      of the plugin api, and so should not be translated.
    */
    errmsg_printf(ERRMSG_LVL_ERROR, _("scheduling plugin '%s' init() failed\n"),
                    plugin->name.str);
      return 1;
  }

  scheduler_inited= true;
  /* We populate so we can find which plugin was initialized later on */
  /*pass in the data */
  plugin->data= (void *)thread_scheduler;
  plugin->state= PLUGIN_IS_READY;

  return 0;

}
/**
 * SYNOPSIS  scheduling_finalizer
 *
 * @param plugin  st_plugin_int*
 * @return    0
 */

int scheduling_finalizer(st_plugin_int* plugin)
{
  /* We know which one we initialized since its data pointer is filled */
  if (plugin->plugin->deinit && plugin->data)
  {
    if (plugin->plugin->deinit((void *)thread_scheduler))
    {
      /* TRANSLATORS: The leading word "scheduling" is the name
         of the plugin api, and so should not be translated. */
      errmsg_printf(ERRMSG_LVL_ERROR,
                    _("scheduling plugin '%s' deinit() failed\n"),
                    plugin->name.str);
    }
  }
  return 0;
}

//  End of scheduling.cc
